#include <QMediaPlaylist>
#include <QScreen>
#include <QWindow>
#include <QDebug>
#include <QVideoWidget>
#include <QMediaPlayer>
#include <QFile>
#include <QGuiApplication>

#include "WallPaper.h"

WallPaper::WallPaper(QWidget *parent) : QWidget(parent)
{
    setWindowFlag(Qt::FramelessWindowHint);
    setWindowState(Qt::WindowFullScreen);

    playlist = new QMediaPlaylist(this);
    playlist->setPlaybackMode(QMediaPlaylist::PlaybackMode::Loop);

    player = new QMediaPlayer(this);
    videoWidget = new QVideoWidget(this);

    player->setPlaylist(playlist);
    player->setVideoOutput(videoWidget);

    // 全屏播放
    QScreen *screen = videoWidget->windowHandle()->screen();
    videoWidget->setGeometry(screen->geometry());
    videoWidget->setAspectRatioMode(Qt::KeepAspectRatioByExpanding); // 拉伸
}

bool WallPaper::setVideo(QString path)
{
    if (QFile::exists(path))
    {
        player->stop();
        playlist->clear();
        playlist->addMedia(QUrl::fromLocalFile(path));
        playlist->setCurrentIndex(0);
        return true;
    }
    else
    {
        return false;
    }
}

void WallPaper::play()
{
    player->play();
}
